import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
import axios from 'axios'
axios.defaults.baseURL = process.env.API_URL
import { setAuthorizationHeader } from '@/common/utilities'

const ADD_REPORTS = 'ADD_REPORTS'
const ADD_REPORT_TO_STACK = 'ADD_REPORT_TO_STACK'
const DELETE_REPORT_FROM_STACK = 'DELETE_REPORT_FROM_STACK'
const EDIT_REPORT_IN_STACK = 'EDIT_REPORT_IN_STACK'

const report = {
    namespaced: true,
    state: {
        reports: []
    },
    mutations: {
        ADD_REPORTS (state, data) {
            state.reports = state.reports.concat(data)
        },
        ADD_REPORT_TO_STACK (state, report) {
            state.reports.unshift(report)
        },
        DELETE_REPORT_FROM_STACK (state, report) {
            if (state.reports.length) {
                let i = state.reports.map(report => report.id).indexOf(report.id)
                state.reports.splice(i, 1)
            }
        },
        EDIT_REPORT_IN_STACK (state, report) {
            if (state.reports.length) {
                let i = state.reports.map(report => report.id).indexOf(report.id)
                Vue.set(state.reports, i, report)
            }
        },
        LOGOUT_USER (state) {
            state.reports = []
        }
    },
    getters: {
        reports (state) {
            return state.reports
        }
    },
    actions: {
        async getUsersReports ({ dispatch, commit, getters, rootGetters }, data) {
            try {
                setAuthorizationHeader(rootGetters['user/accessToken'])
                const response = await axios.get('/api/v1/reports', { params: { sort: data.sort, order: data.order, page: data.page, limit: data.limit } })
                commit(ADD_REPORTS, response.data)
                return response.data
            } catch (error) {
                throw new Error(error)
            }
        },
        async getReport ({ dispatch, commit, getters, rootGetters }, data) {
            try {
                setAuthorizationHeader(rootGetters['user/accessToken'])
                const response = await axios.get('/api/v1/reports/' + data)
                return response.data
            } catch (error) {
                throw new Error(error)
            }
        },
        async createReport ({ dispatch, commit, getters, rootGetters }, data) {
            try {
                setAuthorizationHeader(rootGetters['user/accessToken'])
                return await axios.post('/api/v1/reports/', { title: data.title, todayJob: data.todayJob, todoJob: data.todoJob })
            } catch (error) {
                throw new Error(error)
            }
        },
        async addReportToStack ({ dispatch, commit, getters, rootGetters }, report) {
            try {
                commit(ADD_REPORT_TO_STACK, report)
            } catch (error) {
                throw new Error(error)
            }
        },
        async saveReport ({ dispatch, commit, getters, rootGetters }, data) {
            try {
                setAuthorizationHeader(rootGetters['user/accessToken'])
                return await axios.put('/api/v1/reports/' + data.id, { title: data.title, todayJob: data.todayJob, todoJob: data.todoJob })
            } catch (error) {
                throw new Error(error)
            }
        },
        async deleteReport ({ dispatch, commit, getters, rootGetters }, data) {
            try {
                setAuthorizationHeader(rootGetters['user/accessToken'])
                return await axios.delete('/api/v1/reports/' + data.id)
            } catch (error) {
                throw new Error(error)
            }
        },
        async deleteReportFromStack ({ dispatch, commit, getters, rootGetters }, report) {
            try {
                commit(DELETE_REPORT_FROM_STACK, report)
            } catch (error) {
                throw new Error(error)
            }
        },
        async editReportInStack ({ dispatch, commit, getters, rootGetters }, report) {
            try {
                commit(EDIT_REPORT_IN_STACK, report)
            } catch (error) {
                throw new Error(error)
            }
        }
    }
}

export default report
